package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.DateUtil;
import com.utils.vo.dateLimitUtilVo;
import com.zbkj.crmeb.chant.dao.ChannelMerchantDao;
import com.zbkj.crmeb.chant.request.SyncProductLogsListRequest;
import com.zbkj.crmeb.store.dao.SyncProductLogsDao;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.SyncProductLogs;
import com.zbkj.crmeb.store.service.SyncProductLogsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SyncProductLogsServiceImpl extends ServiceImpl<SyncProductLogsDao, SyncProductLogs> implements SyncProductLogsService {
    @Resource
    private SyncProductLogsDao syncProductLogsDao;

    /**
     * 日志列表
     *
     * @return
     */
    @Override
    public PageInfo<SyncProductLogs> syncProductLogsList(SyncProductLogsListRequest request) {

        LambdaQueryWrapper<SyncProductLogs> lambdaQueryWrapper = getQueryWrapper(request);

        Page<StoreProduct> storeProductPage = PageHelper.startPage(request.getPage(), request.getLimit());
        List<SyncProductLogs> syncProductLogs = syncProductLogsDao.selectList(lambdaQueryWrapper);

        return CommonPage.copyPageInfo(storeProductPage, syncProductLogs);

    }

    private LambdaQueryWrapper<SyncProductLogs> getQueryWrapper(SyncProductLogsListRequest request) {

        //带 StoreProduct 类的多条件查询
        LambdaQueryWrapper<SyncProductLogs> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (request.getGoodsNo() != null) {
            lambdaQueryWrapper.eq(SyncProductLogs::getGoodsNo, request.getGoodsNo());
        }
        if(request.getGoodsName() != null){
            lambdaQueryWrapper.like(SyncProductLogs::getGoodsName,request.getGoodsName());
        }

        if(request.getSkuName() != null){
            lambdaQueryWrapper.like(SyncProductLogs::getSkuName,request.getSkuName());
        }

        if(request.getSkuBarcode() != null){
            lambdaQueryWrapper.eq(SyncProductLogs::getSkuBarcode,request.getSkuBarcode());
        }

        if (request.getCreateTime()!= null) {
            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(request.getCreateTime());
            lambdaQueryWrapper.le(SyncProductLogs::getCreateTime, dateLimit.getEndTime());
            lambdaQueryWrapper.ge(SyncProductLogs::getCreateTime, dateLimit.getStartTime());
        }

        if(request.getCateName() != null){
            lambdaQueryWrapper.like(SyncProductLogs::getCateName,request.getCateName());
        }

        if(request.getBrandName() != null){
            lambdaQueryWrapper.eq(SyncProductLogs::getBrandName,request.getBrandName());
        }

        lambdaQueryWrapper.orderByDesc(SyncProductLogs::getCreateTime);

        return lambdaQueryWrapper;
    }

}
